package br.com.rafa3d.dao;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import br.com.rafa3d.conexao.ConexaoPool;
import br.com.rafa3d.modelo.Acesso;
import br.com.rafa3d.modelo.Cliente;
import br.com.rafa3d.modelo.Pessoa;


public class AcessoDao implements Serializable {

	private static final long serialVersionUID = 1L;
	
	//Connection con = ConexaoSQLServer.getConnection();
	Connection con = ConexaoPool.getConnexao();

	public Object autenticarAcesso(Pessoa loginUsuario) {
		String sql = "SELECT TOP 1 COD_CLIENTE FROM ACESSO WHERE LOGIN_USER = ? AND SENHA = ?";
		Pessoa cliente = new Pessoa(new Acesso());
		PreparedStatement pstm = null;
		try {
			pstm = con.prepareStatement(sql);
			pstm.setString(1, loginUsuario.getAcesso().getLogin());
			pstm.setString(2, loginUsuario.getAcesso().getSenha());
			ResultSet rs = pstm.executeQuery();
			if (rs.next()) {
				cliente.setCod(rs.getInt(1));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch(NullPointerException ex){
			ex.printStackTrace();
		}finally{
			try {
				pstm.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return cliente;
	}

}
